//
//  FriendCircleModel.m
//  ReactCocoaDemo
//
//  Created by letian on 16/12/5.
//  Copyright © 2016年 cmsg. All rights reserved.
//

#import "FriendCircleModel.h"
#import "FriendCircleCommentModel.h"
#import "FriendCircleLikeModel.h"
#import "UILabel+Extension.h"

@implementation FriendCircleModel

+ (NSArray <FriendCircleModel *>*)getDatasFormPlist
{
    NSString * path = [[NSBundle mainBundle]pathForResource:@"Property List.plist" ofType:nil];
    NSArray * data = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray * datas = [NSMutableArray array];
    for (NSDictionary *dic in data) {
        FriendCircleModel * model = [FriendCircleModel new];
        [model setValuesForKeysWithDictionary:dic];
        [datas addObject:model];
    }
    return datas.copy;
}

+ (NSArray *)creatModelsWithCount:(NSInteger)count
{
    NSArray *iconImageNamesArray = @[@"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     ];
    
    NSArray *namesArray = @[@"GSD_iOS",
                            @"风口上的猪",
                            @"当今世界网名都不好起了",
                            @"我叫郭德纲",
                            @"Hello Kitty"];
    
    NSArray *textArray = @[@"当你的 app 没有提供 3x 的 LaunchImage 时，系统默认进入兼容模式，https://github.com/gsdios/SDAutoLayout大屏幕一切按照 320 宽度渲染，屏幕宽度返回 320；然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，等于把小屏完全拉伸。",
                           @"然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，https://github.com/gsdios/SDAutoLayout等于把小屏完全拉伸。",
                           @"当你的 app 没有提供 3x 的 LaunchImage 时屏幕宽度返回 320；然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，等于把小屏完全拉伸。但是建议不要长期处于这种模式下。屏幕宽度返回 320；https://github.com/gsdios/SDAutoLayout然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，等于把小屏完全拉伸。但是建议不要长期处于这种模式下。屏幕宽度返回 320；然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，等于把小屏完全拉伸。但是建议不要长期处于这种模式下。",
                           @"但是建议不要长期处于这种模式下，否则在大屏上会显得字大，内容少，容易遭到用户投诉。",
                           @"屏幕宽度返回 320；https://github.com/gsdios/SDAutoLayout然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，等于把小屏完全拉伸。但是建议不要长期处于这种模式下。"
                           ];
    
    NSArray *commentsArray = @[@"社会主义好！👌👌👌👌 www.baidu.com @你好",
                               @"正宗好凉茶，正宗好声音。。。 www.baidu.com @你好",
                               @"你好，我好，大家好才是真的好 www.baidu.com @你好",
                               @"有意思 www.baidu.com @你好",
                               @"你瞅啥？ www.baidu.com @你好",
                               @"瞅你咋地？？？！！！ www.baidu.com @你好",
                               @"hello，看我 www.baidu.com @你好",
                               @"曾经在幽幽暗暗反反复复中追问，才知道平平淡淡从从容容才是真 www.baidu.com @你好",
                               @"人艰不拆 www.baidu.com @你好",
                               @"咯咯哒 www.baidu.com @你好",
                               @"呵呵~~~~~~~~ www.baidu.com @你好",
                               @"我勒个去，啥世道啊 www.baidu.com @你好",
                               @"真有意思啊你💢💢💢 www.baidu.com @你好"];
    
    NSArray *picImageNamesArray = @[ @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg",
                                     @"http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg"
                                     ];
    NSMutableArray *resArr = [NSMutableArray new];
    
    for (int i = 0; i < count; i++) {
        int iconRandomIndex = arc4random_uniform(5);
        int nameRandomIndex = arc4random_uniform(5);
        int contentRandomIndex = arc4random_uniform(5);
        
        FriendCircleModel *model = [FriendCircleModel new];
        model.icon = iconImageNamesArray[iconRandomIndex];
        model.name = namesArray[nameRandomIndex];
        model.content = textArray[contentRandomIndex];
        model.time = @"2天前";
        
        // 模拟“随机图片”
        int random = arc4random_uniform(6);
        
        NSMutableArray *temp = [NSMutableArray new];
        for (int i = 0; i < random; i++) {
            int randomIndex = arc4random_uniform(9);
            [temp addObject:picImageNamesArray[randomIndex]];
        }
        if (temp.count) {
            model.images = [temp copy];
        }
        
        // 模拟随机评论数据
        int commentRandom = arc4random_uniform(8);
        NSMutableArray *tempComments = [NSMutableArray new];
        for (int i = 0; i < commentRandom; i++) {
            FriendCircleCommentModel *commentItemModel = [FriendCircleCommentModel new];
            
            int index = arc4random_uniform((int)namesArray.count);
            commentItemModel.firstUserName = namesArray[index];
            commentItemModel.firstUserId = @"666";
            if (arc4random_uniform(10) < 5) {
                commentItemModel.secondUserName = namesArray[arc4random_uniform((int)namesArray.count)];
                commentItemModel.secondUserId = @"888";
            }
            commentItemModel.commentString = commentsArray[arc4random_uniform((int)commentsArray.count)];
            commentItemModel.attributedContent = [model generateAttributedStringWithCommentItemModel:commentItemModel];
            
            CGFloat height = [UILabel heightForExpressionText:commentItemModel.attributedContent width:[UIScreen mainScreen].bounds.size.width - 30] + 10;
            if (i == 0) {
                height += 10;
            }
            commentItemModel.cellHeight = height;
             ;
            [tempComments addObject:commentItemModel];
        }
        model.commentDatas = [tempComments copy];
        
        // 模拟随机点赞数据
        int likeRandom = arc4random_uniform(10);
        NSMutableArray *tempLikes = [NSMutableArray new];
        for (int i = 0; i < likeRandom; i++) {
            FriendCircleLikeModel *model = [FriendCircleLikeModel new];
            int index = arc4random_uniform((int)namesArray.count);
            model.userName = namesArray[index];
            model.userId = namesArray[index];
            [tempLikes addObject:model];
        }
        
        model.likeDatas = [tempLikes copy];
        
        
        
        [resArr addObject:model];
    }
    return [resArr copy];
}

///设置评论富文本
- (NSMutableAttributedString *)generateAttributedStringWithCommentItemModel:(FriendCircleCommentModel *)model
{
    NSString *text = model.firstUserName;
    if (model.secondUserName.length) {
        text = [text stringByAppendingString:[NSString stringWithFormat:@"回复%@", model.secondUserName]];
    }
    text = [text stringByAppendingString:[NSString stringWithFormat:@"：%@", model.commentString]];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
    [attString setAttributes:@{NSLinkAttributeName : model.firstUserId} range:[text rangeOfString:model.firstUserName]];
    if (model.secondUserName) {
        [attString setAttributes:@{NSLinkAttributeName : model.secondUserId} range:[text rangeOfString:model.secondUserName]];
    }
    return attString;
}

@end
